package be.groept.ace.enquete.model.beans;

import java.io.Serializable;
import java.util.Date;

public class Answer implements Comparable<Answer>, Serializable {
	private static final long serialVersionUID = 1L;
	private int id;
	
	private String text;
	private Date submitdate;
	
	
	private Question question;
	private User user;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public Date getSubmitdate() {
		return submitdate;
	}
	public void setSubmitdate(Date submitdate) {
		this.submitdate = submitdate;
	}
	
	public Question getQuestion() {
		
		return question;
	}
	public void setQuestion(Question question) throws IllegalArgumentException {
		if(this.question != null) {
			throw new IllegalArgumentException("Answer cannot be updated, question is already set");
		}
		this.question = question;
		//question.addAnswer(this);
	}
	public User getUser()  {
		return user;
	}
	public void setUser(User user) throws IllegalArgumentException {
		if(this.user != null) {
			throw new IllegalArgumentException("Answer cannot be updated, user is already set");
		}
		this.user = user;
		//user.addAnswer(this);
	}
	
	//compare by user and then question
	@Override
	public int compareTo(Answer o) {
		int compare = getUser().compareTo(o.getUser());
		if(compare == 0) 
			compare = getQuestion().compareTo(o.getQuestion());
		return compare;
	}
	
	
	
	@Override
	public int hashCode() {
		return getQuestion().hashCode() * 31 + getUser().hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if( getClass() != obj.getClass()) {
			return false;
		}
		Answer other = (Answer)obj;
		
		return getQuestion().equals(other.getQuestion()) && getUser().equals(other.getUser());
	}
	
	@Override
	public String toString() {
		return String.format("Answer object - id: %1$d - user: %2$s %3$s - question: %4$s", 
										getId(), 
										getUser().getFirstname(), 
										getUser().getName(), 
										getQuestion().getText() );
	}
	
}
